package com.lzb.newloan;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.lzb.newloan.utils.AlertDialog;
import com.lzb.newloan.utils.BaseActivity;
import com.lzb.newloan.utils.Config;
import com.lzb.newloan.utils.HttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class ApplayInvesterActivity extends BaseActivity implements OnClickListener {


	private RadioGroup rg_age;
	private RadioGroup rg2;
	private RadioGroup rg3;
	private RadioGroup rg4;
	private RadioGroup rg5;
	private RadioGroup rg6;
	private RadioGroup rg7;
	private RadioGroup rg8;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_applay_invest);
		initView();
		initData();
	}

	private void initView() {
		findViewById(R.id.backpress).setOnClickListener(this);
		TextView title_txt_center = (TextView) findViewById(R.id.title_txt_center);
		title_txt_center.setText("申请投资人");
		findViewById(R.id.commit_btn).setOnClickListener(this);
		rg_age = (RadioGroup) findViewById(R.id.rg_age);
		rg2 = (RadioGroup) findViewById(R.id.rg2);
		rg3 = (RadioGroup) findViewById(R.id.rg3);
		rg4 = (RadioGroup) findViewById(R.id.rg4);
		rg5 = (RadioGroup) findViewById(R.id.rg5);
		rg6 = (RadioGroup) findViewById(R.id.rg6);
		rg7 = (RadioGroup) findViewById(R.id.rg7);
		rg8 = (RadioGroup) findViewById(R.id.rg8);

	}


	private void initData() {

	}


	@Override
	public void processMessage(Message message) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.backpress:
				finish();
				break;
			case  R.id.commit_btn:
				if (rg_age.getCheckedRadioButtonId()==-1||rg2.getCheckedRadioButtonId()==-1||rg3.getCheckedRadioButtonId()==-1||
						rg4.getCheckedRadioButtonId()==-1||rg5.getCheckedRadioButtonId()==-1||
						rg6.getCheckedRadioButtonId()==-1||rg7.getCheckedRadioButtonId()==-1||rg8.getCheckedRadioButtonId()==-1) {
					showDialog("请先填写完所有问题");
					return;
				}

				if (rg_age.getCheckedRadioButtonId()==R.id.age_no||rg2.getCheckedRadioButtonId()==R.id.rb2_yes||rg3.getCheckedRadioButtonId()==R.id.rb3_yes||
						rg4.getCheckedRadioButtonId()==R.id.rb4_yes||rg5.getCheckedRadioButtonId()==R.id.rb5_yes||
						rg6.getCheckedRadioButtonId()==R.id.rb6_no||rg7.getCheckedRadioButtonId()==R.id.rb7_no||rg8.getCheckedRadioButtonId()==R.id.rb8_no) {
					showDialog("对不起，您还不具备钱宝包贷的投资人条件");
					return;
				}

				SharedPreferences sp = getSharedPreferences("config", 0x0000);
				String userId = sp.getString("userid", "");


				HttpUtils.doPostAsyn(Config.COMMITINVESTER_CORD + "&userid=" + userId, "",mHandler,
						Config.CODE_INVESTAPPLAY);


				break;
			default:
				break;
		}
	}

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
				case Config.CODE_URL_ERROR:
					Toast.makeText(ApplayInvesterActivity.this, "url错误", Toast.LENGTH_SHORT)
							.show();
					break;
				case Config.CODE_NET_ERROR:
					Toast.makeText(ApplayInvesterActivity.this, "网络错误", Toast.LENGTH_SHORT)
							.show();
					break;

				case Config.CODE_INVESTAPPLAY:
					String result = msg.obj.toString();
					try {
						JSONObject json = new JSONObject(result);
						int error = Integer.parseInt(json
								.getString("error"));
						String msgs = json.getString("msg");
						if (error==-3) {
							startActivity(new Intent(
									ApplayInvesterActivity.this,
									InvestPersonActivity.class));
						}else {
							new AlertDialog(ApplayInvesterActivity.this).builder().setMsg(msgs)
									.setNegativeButton("确定", new OnClickListener() {
										@Override
										public void onClick(View v) {
											startActivity(new Intent(
													ApplayInvesterActivity.this,
												IndexActivity.class));

										}
									}).show();
						}
						Toast.makeText(ApplayInvesterActivity.this,msgs,Toast.LENGTH_LONG).show();;
						finish();
					} catch (JSONException e) {
						Toast.makeText(ApplayInvesterActivity.this, "数据解析错误",
								Toast.LENGTH_SHORT).show();
						e.printStackTrace();
					}

					break;
				default:
					break;
			}
		};
	};

	private void showDialog(String message) {
		new AlertDialog(ApplayInvesterActivity.this)
				.builder()
				.setMsg(message)
				.setNegativeButton("确定",
						new OnClickListener() {
							@Override
							public void onClick(
									View v) {
							}
						}).show();
	}

}
